package com.hp.java12_springsecurity.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @RequestMapping("login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("toMain")
    public String toMain(Model model){
        //获得当前的验证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object obj = authentication.getPrincipal();
        if(obj instanceof User){
            User user = (User) obj;
            model.addAttribute("username",user.getUsername());
            model.addAttribute("authorities",user.getAuthorities());
        }
        return "main";
    }

    @RequestMapping("/admin/admin")
    public String admin(){
        return "admin/admin";
    }

    @RequestMapping("/user/user")
    public String user(){
        return "user/user";
    }
}
